package com.yyl.jni.player;

import android.text.TextUtils;
import android.util.Log;

import com.yyl.jni.listener.OnEventListener;
import com.yyl.jni.opengl.VideoSurfaceView;
import com.yyl.jni.utils.VideoSupportUtils;

import java.io.File;

public class YPlayer {
    private AudioPcmEncoder pcmEncoder;
    private VideoYUVCodec videoYUVCodec;
    private String source;
    OnEventListener onEventListener;
    private VideoSurfaceView surfaceView;
    private boolean isExitState = false;

    public YPlayer() {
        pcmEncoder = new AudioPcmEncoder(this);
        videoYUVCodec=new VideoYUVCodec(this);
    }

    public void setSurfaceView(VideoSurfaceView surfaceView) {
        this.surfaceView = surfaceView;
        videoYUVCodec.setSurfaceView(surfaceView);
    }


    public void prepared() {
        if (TextUtils.isEmpty(source)) {
            return;
        }
        if (isExitState) {
            return;
        }
        new Thread(new Runnable() {
            @Override
            public void run() {
                prepare(source);
            }
        }).start();

    }

    public void start() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                play();
            }
        }).start();
    }

    public void playChange(boolean isPlay) {
        changePlayState(isPlay);
    }

    public void onStop() {
        isExitState = true;
        new Thread(new Runnable() {
            @Override
            public void run() {
                stop();
                videoYUVCodec.releaseMediacodec();
                isExitState = false;
            }
        }).start();
    }

    private native void play();

    private native void prepare(String source);


    private native void changePlayState(boolean isPlay);

    private native void stop();

    private native void seek(long time);


    public native long getDuration();

    public native double getCurrentTime();

    public void pauseRecord() {
        record(false);
        d("暂停录制");
    }


    public void resumeRcord() {
        record(true);
        d("继续录制");
    }

    public void startRecord(File outfile) {
        pcmEncoder.startRecord(outfile);
    }

    public void stopRecord() {
        pcmEncoder.stopRecord();
    }

    /**
     * @param option 1  setPitch(double)  2 setSpeed(double)  3 setMute(int)  4 setVolume(float)
     * @param value  int
     * @param fvalue float
     */
    private native void setAudioOption(int option, int value, double fvalue);

    public void setPitch(double value) {
        setAudioOption(1, 0, value);
    }

    public void setSpeed(double value) {
        setAudioOption(2, 0, value);
    }

    public void setMute(int value) {
        setAudioOption(3, value, 0d);
    }

    public void setVolume(int value) {
        setAudioOption(4, value, 0d);
    }

    public void seekTo(long value) {
        seek(value);
    }


    public void setSource(String source) {
        this.source = source;
    }

    /**
     * #define EVENT_STATE_PARPARED 0
     * #define EVENT_STATE_PLAY 1
     * #define EVENT_STATE_TIME 2
     * #define EVENT_STATE_COMPLETE 3
     * #define EVENT_STATE_PCM_DB 4  //pcm 分贝值
     * #define EVENT_STATE_ERROR 10
     *
     * @param event
     * @param arg1
     * @param arg2
     */
    public void onJNICallBackEvent(int event, int arg1, int arg2) {
        if (onEventListener != null) {
            onEventListener.onEvent(event, arg1, arg2);
        }
    }

    private void onJNICallPcmToAAC(int event, int arg1, byte[] buffer) {
        pcmEncoder.onJNICallPcmToAAC(event, arg1, buffer);
    }

    public void onJNICallRenderYUV(int width, int height, byte[] y, byte[] u, byte[] v) {
        if (surfaceView != null) {
            surfaceView.setYUVData(width, height, y, u, v);
        }
    }

    public boolean onJNISupportHardware(String ffcodecname) {
        return VideoSupportUtils.isSupportCodec(ffcodecname);
    }

    public void onJNIdecodeAVPacket(int datasize, byte[] data) {
        videoYUVCodec.onJNIdecodeAVPacket(datasize, data);
    }

    public boolean onJNIInitMediaCodec(String codecName, int width, int height, byte[] csd_0, byte[] csd_1) {
        return videoYUVCodec.onJNIInitMediaCodec(codecName, width, height, csd_0, csd_1);
    }

    public void setOnEventListener(OnEventListener onEventListener) {
        this.onEventListener = onEventListener;
    }


    native int n_samplerate();

    native void record(boolean isRecord);


    public void d(String msg) {
        Log.i("yyl", msg);
    }


}
